/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller.project;

import java.util.ArrayList;
import java.sql.Date;
import java.util.List;
import org.apache.log4j.Logger;
import queenb.net.entitybean.B030;
import queenb.net.entitybean.C650;
import queenb.web.controller.QueenbController;
import queenb.web.store.B030Store;
import queenb.web.store.C650Store;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author VANANH
 */
public class PersonalProjectController extends QueenbController {

    private static Logger logger = Logger.getLogger(PersonalProjectController.class);
    private B030Store b030Store;
    private C650Store c650Store;
    private int pb050;
    private int acturnReturnCode;
    private int personProject;
    private String getListB030Left;
    private String getListB030Right;
    private String listC650Left;
    private String listC650Right;
    private String fc150;
    private Date cd651;
    private int fb050;
    private int fc500;
    private String emp;

    public PersonalProjectController() {
        b030Store = new B030Store();
        c650Store = new C650Store();

        try {
            Object b050Object = getRequest().getParameter("pb050");
            System.out.println("---------------pb050-------------------"+b050Object);
            if (b050Object != null) {
                this.pb050 = Util.parseInt(b050Object);
                SessionProvider.setSessionMapValue("PB050", pb050);
            }
            Object objFc150 = getRequest().getParameter("fc150");
            if (objFc150 != null) {
                fc150 = objFc150.toString();
            }
            Object objCd651 = getRequest().getParameter("cd651");
            if (objCd651 != null) {
                cd651 = Util.stringToSQLDate(objCd651.toString(), "yyyy-MM-dd");
            }
            Object objFb050 = getRequest().getParameter("fb050");
            if (objFb050 != null) {
                fb050 = Integer.parseInt(objFb050.toString());
            }
            Object objFc500 = getRequest().getParameter("fc500");
            if (objFc500 != null) {
                fc500 = Integer.parseInt(objFc500.toString());
            }
            Object objEmp = getRequest().getParameter("emp");
            if(objEmp !=null){
                emp = objEmp.toString();
            }else{
                emp = getLogin().getUserName();
            }
            System.out.println("----fc150--:" + fc150 + "---cd651--" + cd651 + "----fb050--:" + fb050 + "---fc500--:" + fc500);
        } catch (Exception e) {
        }


    }

    public int getActurnReturnCode() {

        ArrayList<Integer> front = new ArrayList<Integer>();
        ArrayList<Integer> back = new ArrayList<Integer>();
        if (SessionProvider.getSessionMapValue("PB050") != null) {
            pb050 = Integer.parseInt(SessionProvider.getSessionMapValue("PB050").toString());
        }

        Object isupdate = getRequest().getParameter("values2");

        if (isupdate != null) {

            String[] array = isupdate.toString().split(",");
            logger.debug("+++++" + isupdate + "++" + array.length + "--" + this.pb050);
            for (int i = 0; i < array.length; i++) {
                front.add(Integer.parseInt(array[i]));
            }
            //get right list
            if (SessionProvider.getSessionMapValue("PB050") != null) {
                pb050 = Integer.parseInt(SessionProvider.getSessionMapValue("PB050").toString());
            }

            List<B030> list = new ArrayList<B030>();
            try {

                list = b030Store.getListB030R(getLogin().getModule(), getLogin().getCompanyNo(), this.pb050, 0, getLogin().getUserName());
                System.out.println("KIch thuot list ben phai co id= " + pb050 + " la: " + list.size());

            } catch (Exception ex) {
                System.out.println("-------------. ERROR CODE: " + ex.getMessage());
                //showGlobalReport(true, getResourceString("updatefail"));
                return 0;
            }
            for (int i = 0; i < list.size(); i++) {
                back.add(list.get(i).getPN100());
            }
            logger.debug("size" + front.size() + "size" + back.size());

            for (int i = 0; i < front.size(); i++) {
                if (front.get(i) == 0) {
                    continue;
                }

                int result = 0;
                if (!back.contains(front.get(i)) || back.size() == 0) {
                    try {


                        logger.debug("updateeeeeeeeeee" + this.pb050 + "--" + front.get(i));
                        b030Store.updateB030(getLogin().getModule(), getLogin().getCompanyNo(), this.pb050, front.get(i), 0, null, getLogin().getUserName());
                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage() + result);
                        return 0;
                    }
                }
            }
            for (int i = 0; i < back.size(); i++) {
                logger.debug("delete " + back.get(i) + front.contains(back.get(i)));
                if (!front.contains(back.get(i))) {
                    try {

                        logger.debug("delete " + back.get(i) + "with" + this.pb050);
                        b030Store.deleteB030(this.pb050, back.get(i), getLogin().getUserName());

                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage());
                        return 0;
                    }
                }
            }

        }
        // showGlobalReport(true, getResourceString("updatesuccess"));

        return acturnReturnCode;
    }

    public int getPersonProject() {

        ArrayList<Integer> left = new ArrayList<Integer>();
        ArrayList<Integer> right = new ArrayList<Integer>();
//        if (SessionProvider.getSessionMapValue("PB050") != null) {
//            pb050 = Integer.parseInt(SessionProvider.getSessionMapValue("PB050").toString());
//        }

        Object isupdate = getRequest().getParameter("values2");
        System.out.println("-------------. isupdate: " + isupdate);
        if (isupdate != null) {

            String[] array = isupdate.toString().split(",");
            logger.debug("+++++" + isupdate + "++" + array.length + "--" + this.fc150);
            for (int i = 0; i < array.length; i++) {
                left.add(Integer.parseInt(array[i]));
            }
            //get right list
//            if (SessionProvider.getSessionMapValue("PB050") != null) {
//                pb050 = Integer.parseInt(SessionProvider.getSessionMapValue("PB050").toString());
//            }

            List<C650> list = new ArrayList<C650>();
            try {

                list = c650Store.LISTOFTABC650R(getLogin().getModule(), fc150, 0, getLogin().getUserName());
                System.out.println("KIch thuot list ben trái co id= " + fc150 + " la: " + list.size());


            } catch (Exception ex) {
                System.out.println("-------------. ERROR CODE: " + ex.getMessage());
                //showGlobalReport(true, getResourceString("updatefail"));
                return 0;
            }
            for (int i = 0; i < list.size(); i++) {
                right.add(list.get(i).getPn100());
            }
            logger.debug("size" + left.size() + "size" + right.size());

            for (int i = 0; i < left.size(); i++) {
                if (left.get(i) == 0) {
                    continue;
                }

                int result = 0;
                if (!right.contains(left.get(i)) || right.size() == 0) {
                    try {


                       System.out.println("updateeeeeeeeeee" + this.pb050 + "--" + left.get(i));
                         System.out.println("rs insert --------:" + getLogin().getModule()+","+ getLogin().getCompanyNo()+","+ getFb050()+","+ left.get(i)+","+ fc150+","+ getFc500()+","+
                                null+","+ 0+","+ 0+","+ getLogin().getUserName());
                        int rs = c650Store.INSERTTABC650(getLogin().getModule(), getLogin().getCompanyNo(), getFb050(), left.get(i), fc150, getFc500(),
                                null, 0, 0, getLogin().getUserName());
                       
                        //                        b030Store.updateB030(getLogin().getModule(), getLogin().getCompanyNo(), this.pb050, front.get(i), 0, null, getLogin().getUserName());
                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage() + result);
                        return 0;
                    }
                }
            }
            for (int i = 0; i < right.size(); i++) {
                logger.debug("delete " + right.get(i) + left.contains(right.get(i)));
                if (!left.contains(right.get(i))) {
                    try {

                        System.out.println("delete " + right.get(i) + "with" + fc150);
                        int result = c650Store.StornoTabC650(getLogin().getModule(), getLogin().getCompanyNo(), right.get(i), fc150, getLogin().getUserName());
                        System.out.println("rs update --------:" + result);
                        //                        b030Store.deleteB030(this.pb050, back.get(i), getLogin().getUserName());

                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage());
                        return 0;
                    }
                }
            }
            
        }
        // showGlobalReport(true, getResourceString("updatesuccess"));

        return personProject;
    }

    public List<B030> getListB030L() {
        try {
            List<B030> listB050L = b030Store.getListB030L(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, getLogin().getUserName());
            return listB050L;
        } catch (Exception e) {
            return null;
        }
    }

    public List<B030> getListB030R() {
        try {
            List<B030> listB050R = b030Store.getListB030R(getLogin().getModule(), getLogin().getCompanyNo(), this.pb050, 0, getLogin().getUserName());
            return listB050R;
        } catch (Exception e) {
            return null;
        }
    }

    public List<C650> getListC650R() {

        try {
            List<C650> lstC650R = c650Store.LISTOFTABC650R(getLogin().getModule(), fc150, 0, getLogin().getUserName());
            return lstC650R;
        } catch (Exception e) {
            return null;
        }

    }

    public List<C650> getListC650L() {
        try {
            List<C650> lstC650L = c650Store.LISTOFTABC650L(getLogin().getModule(), fc150, 0, getLogin().getUserName());
            return lstC650L;
        } catch (Exception e) {
            return null;
        }
    }

    public String getListB030right() {
        List<B030> list = null;
        String right = "";
        try {
            list = getListB030R();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }
//
        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getFN850();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getFN850());
                        right += "<div title='" + n + "' ondblclick='moveLeft(this)' onclick='expand(this)' class='group rgroupparent" + list.get(0).getFN850() + "'>" + list.get(0).getNV852() + "</div>";
                        right += "<div style='display:none' class='child rgroupchild" + list.get(0).getFN850() + "'>";



                        right += "<div class='destinationItem ' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + list.get(0).getPN100() + "' title='" + n + "'>";
                        right += list.get(0).getHOTEN();
                        right += "</div>";

                        flat = 1;
                        list.remove(0);
                    } else {

                        B030 b030 = list.get(i);
                        if (b030.getFN850() == n) {

                            right += "<div class='destinationItem' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + b030.getPN100() + "' title='" + n + "'>";
                            right += b030.getHOTEN();
                            right += "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                right += "</div>";
            }


        }
//
        return right;
    }

    public String getListB030Left() {

        List<B030> list = null;
        String txtLeftString = "";
        String left = "";
        try {
            list = getListB030L();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }

        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getFN850();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getPN100());
                        left += "<div title='" + n + "' ondblclick='moveRight(this)' onclick='expand(this)' class='group lgroupparent" + list.get(0).getFN850() + "'>" + list.get(0).getNV852() + "</div>";
                        left += "<div style='display:none' class='child lgroupchild" + list.get(0).getFN850() + "'>";

                        left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                + list.get(0).getPN100() + "' title='" + n + "'>" + list.get(0).getHOTEN() + "</div>";
                        flat = 1;
                        list.remove(0);
                    } else {

                        B030 b030 = list.get(i);
                        if (b030.getFN850() == n) {

                            left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                    + b030.getPN100() + "' title='" + n + "' >" + b030.getHOTEN() + "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                left += "</div>";
            }
        }
        return left;
    }

    /**
     * @param getListB030Left the getListB030Left to set
     */
    public void setGetListB030Left(String getListB030Left) {
        this.getListB030Left = getListB030Left;
    }

    /**
     * @return the pb050
     */
    public int getPb050() {
        return pb050;
    }

    /**
     * @param pb050 the pb050 to set
     */
    public void setPb050(int pb050) {
        this.pb050 = pb050;
    }

    /**
     * @param acturnReturnCode the acturnReturnCode to set
     */
    public void setActurnReturnCode(int acturnReturnCode) {
        this.acturnReturnCode = acturnReturnCode;
    }

    /**
     * @return the listC650Left
     */
    public String getListC650Left() {
        List<C650> list = null;
        String txtLeftString = "";
        String left = "";
        try {
            list = getListC650L();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }

        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getPn850();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getPn100());
                        left += "<div title='" + n + "' ondblclick='moveRight(this)' onclick='expand(this)' class='group lgroupparent" + list.get(0).getPn850() + "'>" + list.get(0).getNv852() + "</div>";
                        left += "<div style='display:none' class='child lgroupchild" + list.get(0).getPn850() + "'>";

                        left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                + list.get(0).getPn100() + "' title='" + n + "'>" + list.get(0).getHoten() + "</div>";
                        flat = 1;
                        list.remove(0);
                    } else {

                        C650 c650 = list.get(i);
                        if (c650.getPn850() == n) {

                            left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                    + c650.getPn100() + "' title='" + n + "' >" + c650.getHoten() + "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                left += "</div>";
            }
        }
        return left;
    }

    /**
     * @param listC650Left the listC650Left to set
     */
    public void setListC650Left(String listC650Left) {
        this.listC650Left = listC650Left;
    }

    /**
     * @return the listC650Right
     */
    public String getListC650Right() {
        List<C650> list = null;
        String right = "";
        try {
            list = getListC650R();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }
//
        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getPn850();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getPn850());
                        right += "<div title='" + n + "' ondblclick='moveLeft(this)' onclick='expand(this)' class='group rgroupparent" + list.get(0).getPn850() + "'>" + list.get(0).getNv852() + "</div>";
                        right += "<div style='display:none' class='child rgroupchild" + list.get(0).getPn850() + "'>";



                        right += "<div class='destinationItem ' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + list.get(0).getPn100() + "' title='" + n + "'>";
                        right += list.get(0).getHoten();
                        right += "</div>";

                        flat = 1;
                        list.remove(0);
                    } else {

                        C650 c650 = list.get(i);
                        if (c650.getPn850() == n) {

                            right += "<div class='destinationItem' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + c650.getPn100() + "' title='" + n + "'>";
                            right += c650.getHoten();
                            right += "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                right += "</div>";
            }


        }
//
        return right;
    }

    /**
     * @param listC650Right the listC650Right to set
     */
    public void setListC650Right(String listC650Right) {
        this.listC650Right = listC650Right;
    }

    /**
     * @param personProject the personProject to set
     */
    public void setPersonProject(int personProject) {
        this.personProject = personProject;
    }

    /**
     * @return the fc150
     */
    public String getFc150() {
        return fc150;
    }

    /**
     * @param fc150 the fc150 to set
     */
    public void setFc150(String fc150) {
        this.fc150 = fc150;
    }

    /**
     * @return the cd651
     */
    public Date getCd651() {
        return cd651;
    }

    /**
     * @param cd651 the cd651 to set
     */
    public void setCd651(Date cd651) {
        this.cd651 = cd651;
    }

    /**
     * @return the fb050
     */
    public int getFb050() {
        return fb050;
    }

    /**
     * @param fb050 the fb050 to set
     */
    public void setFb050(int fb050) {
        this.fb050 = fb050;
    }

    /**
     * @return the fc500
     */
    public int getFc500() {
        return fc500;
    }

    /**
     * @param fc500 the fc500 to set
     */
    public void setFc500(int fc500) {
        this.fc500 = fc500;
    }

    /**
     * @return the emp
     */
    public String getEmp() {
        return emp;
    }

    /**
     * @param emp the emp to set
     */
    public void setEmp(String emp) {
        this.emp = emp;
    }
}
